<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>8.7. textwrap — Text wrapping and filling &mdash; Python v2.6.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.6.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.6.2 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.6.2 documentation" href="../index.html" />
    <link rel="up" title="8. String Services" href="strings.html" />
    <link rel="next" title="8.8. codecs — Codec registry and base classes" href="codecs.html" />
    <link rel="prev" title="8.5. StringIO — Read and write strings as files" href="stringio.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
 

  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="codecs.html" title="8.8. codecs — Codec registry and base classes"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="stringio.html" title="8.5. StringIO — Read and write strings as files"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" accesskey="U">8. String Services</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-textwrap">
<h1>8.7. <tt class="xref docutils literal"><span class="pre">textwrap</span></tt> &#8212; Text wrapping and filling<a class="headerlink" href="#module-textwrap" title="Permalink to this headline">¶</a></h1>
<p>
<span class="versionmodified">New in version 2.3.</span></p>
<p>The <tt class="xref docutils literal"><span class="pre">textwrap</span></tt> module provides two convenience functions, <a title="textwrap.wrap" class="reference internal" href="#textwrap.wrap"><tt class="xref docutils literal"><span class="pre">wrap()</span></tt></a> and
<a title="textwrap.fill" class="reference internal" href="#textwrap.fill"><tt class="xref docutils literal"><span class="pre">fill()</span></tt></a>, as well as <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a>, the class that does all the work,
and a utility function  <a title="textwrap.dedent" class="reference internal" href="#textwrap.dedent"><tt class="xref docutils literal"><span class="pre">dedent()</span></tt></a>.  If you&#8217;re just wrapping or filling one
or two  text strings, the convenience functions should be good enough;
otherwise,  you should use an instance of <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> for efficiency.</p>
<dl class="function">
<dt id="textwrap.wrap">
<tt class="descclassname">textwrap.</tt><tt class="descname">wrap</tt><big>(</big><em>text</em><span class="optional">[</span>, <em>width</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#textwrap.wrap" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the single paragraph in <em>text</em> (a string) so every line is at most <em>width</em>
characters long.  Returns a list of output lines, without final newlines.</p>
<p>Optional keyword arguments correspond to the instance attributes of
<a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a>, documented below.  <em>width</em> defaults to <tt class="docutils literal"><span class="pre">70</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="textwrap.fill">
<tt class="descclassname">textwrap.</tt><tt class="descname">fill</tt><big>(</big><em>text</em><span class="optional">[</span>, <em>width</em><span class="optional">[</span>, <em>...</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#textwrap.fill" title="Permalink to this definition">¶</a></dt>
<dd><p>Wraps the single paragraph in <em>text</em>, and returns a single string containing the
wrapped paragraph.  <a title="textwrap.fill" class="reference internal" href="#textwrap.fill"><tt class="xref docutils literal"><span class="pre">fill()</span></tt></a> is shorthand for</p>
<div class="highlight-python"><div class="highlight"><pre><span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">wrap</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="o">...</span><span class="p">))</span>
</pre></div>
</div>
<p>In particular, <a title="textwrap.fill" class="reference internal" href="#textwrap.fill"><tt class="xref docutils literal"><span class="pre">fill()</span></tt></a> accepts exactly the same keyword arguments as
<a title="textwrap.wrap" class="reference internal" href="#textwrap.wrap"><tt class="xref docutils literal"><span class="pre">wrap()</span></tt></a>.</p>
</dd></dl>

<p>Both <a title="textwrap.wrap" class="reference internal" href="#textwrap.wrap"><tt class="xref docutils literal"><span class="pre">wrap()</span></tt></a> and <a title="textwrap.fill" class="reference internal" href="#textwrap.fill"><tt class="xref docutils literal"><span class="pre">fill()</span></tt></a> work by creating a <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a>
instance and calling a single method on it.  That instance is not reused, so for
applications that wrap/fill many text strings, it will be more efficient for you
to create your own <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> object.</p>
<p>Text is preferably wrapped on whitespaces and right after the hyphens in
hyphenated words; only then will long words be broken if necessary, unless
<a title="textwrap.TextWrapper.break_long_words" class="reference internal" href="#textwrap.TextWrapper.break_long_words"><tt class="xref docutils literal"><span class="pre">TextWrapper.break_long_words</span></tt></a> is set to false.</p>
<p>An additional utility function, <a title="textwrap.dedent" class="reference internal" href="#textwrap.dedent"><tt class="xref docutils literal"><span class="pre">dedent()</span></tt></a>, is provided to remove
indentation from strings that have unwanted whitespace to the left of the text.</p>
<dl class="function">
<dt id="textwrap.dedent">
<tt class="descclassname">textwrap.</tt><tt class="descname">dedent</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#textwrap.dedent" title="Permalink to this definition">¶</a></dt>
<dd><p>Remove any common leading whitespace from every line in <em>text</em>.</p>
<p>This can be used to make triple-quoted strings line up with the left edge of the
display, while still presenting them in the source code in indented form.</p>
<p>Note that tabs and spaces are both treated as whitespace, but they are not
equal: the lines <tt class="docutils literal"><span class="pre">&quot;</span>&nbsp; <span class="pre">hello&quot;</span></tt> and <tt class="docutils literal"><span class="pre">&quot;\thello&quot;</span></tt> are considered to have no
common leading whitespace.  (This behaviour is new in Python 2.5; older versions
of this module incorrectly expanded tabs before searching for common leading
whitespace.)</p>
<p>For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">test</span><span class="p">():</span>
    <span class="c"># end first line with \ to avoid the empty line!</span>
    <span class="n">s</span> <span class="o">=</span> <span class="s">&#39;&#39;&#39;</span><span class="se">\</span>
<span class="s">    hello</span>
<span class="s">      world</span>
<span class="s">    &#39;&#39;&#39;</span>
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>          <span class="c"># prints &#39;    hello\n      world\n    &#39;</span>
    <span class="k">print</span> <span class="nb">repr</span><span class="p">(</span><span class="n">dedent</span><span class="p">(</span><span class="n">s</span><span class="p">))</span>  <span class="c"># prints &#39;hello\n  world\n&#39;</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="textwrap.TextWrapper">
<em class="property">
class </em><tt class="descclassname">textwrap.</tt><tt class="descname">TextWrapper</tt><big>(</big><em>...</em><big>)</big><a class="headerlink" href="#textwrap.TextWrapper" title="Permalink to this definition">¶</a></dt>
<dd><p>The <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> constructor accepts a number of optional keyword
arguments.  Each argument corresponds to one instance attribute, so for example</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">(</span><span class="n">initial_indent</span><span class="o">=</span><span class="s">&quot;* &quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>is the same as</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">wrapper</span> <span class="o">=</span> <span class="n">TextWrapper</span><span class="p">()</span>
<span class="n">wrapper</span><span class="o">.</span><span class="n">initial_indent</span> <span class="o">=</span> <span class="s">&quot;* &quot;</span>
</pre></div>
</div>
<p>You can re-use the same <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> object many times, and you can
change any of its options through direct assignment to instance attributes
between uses.</p>
<p>The <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> instance attributes (and keyword arguments to the
constructor) are as follows:</p>
<dl class="attribute">
<dt id="textwrap.TextWrapper.width">
<tt class="descname">width</tt><a class="headerlink" href="#textwrap.TextWrapper.width" title="Permalink to this definition">¶</a></dt>
<dd>(default: <tt class="docutils literal"><span class="pre">70</span></tt>) The maximum length of wrapped lines.  As long as there
are no individual words in the input text longer than <a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a>,
<a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> guarantees that no output line will be longer than
<a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a> characters.</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.expand_tabs">
<tt class="descname">expand_tabs</tt><a class="headerlink" href="#textwrap.TextWrapper.expand_tabs" title="Permalink to this definition">¶</a></dt>
<dd>(default: <tt class="xref docutils literal"><span class="pre">True</span></tt>) If true, then all tab characters in <em>text</em> will be
expanded to spaces using the <tt class="xref docutils literal"><span class="pre">expandtabs()</span></tt> method of <em>text</em>.</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.replace_whitespace">
<tt class="descname">replace_whitespace</tt><a class="headerlink" href="#textwrap.TextWrapper.replace_whitespace" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="xref docutils literal"><span class="pre">True</span></tt>) If true, each whitespace character (as defined by
<tt class="docutils literal"><span class="pre">string.whitespace</span></tt>) remaining after tab expansion will be replaced by a
single space.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If <a title="textwrap.TextWrapper.expand_tabs" class="reference internal" href="#textwrap.TextWrapper.expand_tabs"><tt class="xref docutils literal"><span class="pre">expand_tabs</span></tt></a> is false and <a title="textwrap.TextWrapper.replace_whitespace" class="reference internal" href="#textwrap.TextWrapper.replace_whitespace"><tt class="xref docutils literal"><span class="pre">replace_whitespace</span></tt></a> is true,
each tab character will be replaced by a single space, which is <em>not</em>
the same as tab expansion.</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.drop_whitespace">
<tt class="descname">drop_whitespace</tt><a class="headerlink" href="#textwrap.TextWrapper.drop_whitespace" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="xref docutils literal"><span class="pre">True</span></tt>) If true, whitespace that, after wrapping, happens to
end up at the beginning or end of a line is dropped (leading whitespace in
the first line is always preserved, though).</p>
<p>
<span class="versionmodified">New in version 2.6: </span>Whitespace was always dropped in earlier versions.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.initial_indent">
<tt class="descname">initial_indent</tt><a class="headerlink" href="#textwrap.TextWrapper.initial_indent" title="Permalink to this definition">¶</a></dt>
<dd>(default: <tt class="docutils literal"><span class="pre">''</span></tt>) String that will be prepended to the first line of
wrapped output.  Counts towards the length of the first line.</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.subsequent_indent">
<tt class="descname">subsequent_indent</tt><a class="headerlink" href="#textwrap.TextWrapper.subsequent_indent" title="Permalink to this definition">¶</a></dt>
<dd>(default: <tt class="docutils literal"><span class="pre">''</span></tt>) String that will be prepended to all lines of wrapped
output except the first.  Counts towards the length of each line except
the first.</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.fix_sentence_endings">
<tt class="descname">fix_sentence_endings</tt><a class="headerlink" href="#textwrap.TextWrapper.fix_sentence_endings" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="xref docutils literal"><span class="pre">False</span></tt>) If true, <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> attempts to detect
sentence endings and ensure that sentences are always separated by exactly
two spaces.  This is generally desired for text in a monospaced font.
However, the sentence detection algorithm is imperfect: it assumes that a
sentence ending consists of a lowercase letter followed by one of <tt class="docutils literal"><span class="pre">'.'</span></tt>,
<tt class="docutils literal"><span class="pre">'!'</span></tt>, or <tt class="docutils literal"><span class="pre">'?'</span></tt>, possibly followed by one of <tt class="docutils literal"><span class="pre">'&quot;'</span></tt> or <tt class="docutils literal"><span class="pre">&quot;'&quot;</span></tt>,
followed by a space.  One problem with this is algorithm is that it is
unable to detect the difference between &#8220;Dr.&#8221; in</p>
<div class="highlight-python"><pre>[...] Dr. Frankenstein's monster [...]</pre>
</div>
<p>and &#8220;Spot.&#8221; in</p>
<div class="highlight-python"><pre>[...] See Spot. See Spot run [...]</pre>
</div>
<p><a title="textwrap.TextWrapper.fix_sentence_endings" class="reference internal" href="#textwrap.TextWrapper.fix_sentence_endings"><tt class="xref docutils literal"><span class="pre">fix_sentence_endings</span></tt></a> is false by default.</p>
<p>Since the sentence detection algorithm relies on <tt class="docutils literal"><span class="pre">string.lowercase</span></tt> for
the definition of &#8220;lowercase letter,&#8221; and a convention of using two spaces
after a period to separate sentences on the same line, it is specific to
English-language texts.</p>
</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.break_long_words">
<tt class="descname">break_long_words</tt><a class="headerlink" href="#textwrap.TextWrapper.break_long_words" title="Permalink to this definition">¶</a></dt>
<dd>(default: <tt class="xref docutils literal"><span class="pre">True</span></tt>) If true, then words longer than <a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a> will be
broken in order to ensure that no lines are longer than <a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a>.  If
it is false, long words will not be broken, and some lines may be longer
than <a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a>.  (Long words will be put on a line by themselves, in
order to minimize the amount by which <a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a> is exceeded.)</dd></dl>

<dl class="attribute">
<dt id="textwrap.TextWrapper.break_on_hyphens">
<tt class="descname">break_on_hyphens</tt><a class="headerlink" href="#textwrap.TextWrapper.break_on_hyphens" title="Permalink to this definition">¶</a></dt>
<dd><p>(default: <tt class="xref docutils literal"><span class="pre">True</span></tt>) If true, wrapping will occur preferably on whitespaces
and right after hyphens in compound words, as it is customary in English.
If false, only whitespaces will be considered as potentially good places
for line breaks, but you need to set <a title="textwrap.TextWrapper.break_long_words" class="reference internal" href="#textwrap.TextWrapper.break_long_words"><tt class="xref docutils literal"><span class="pre">break_long_words</span></tt></a> to false if
you want truly insecable words.  Default behaviour in previous versions
was to always allow breaking hyphenated words.</p>
<p>
<span class="versionmodified">New in version 2.6.</span></p>
</dd></dl>

<p><a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> also provides two public methods, analogous to the
module-level convenience functions:</p>
<dl class="method">
<dt id="textwrap.TextWrapper.wrap">
<tt class="descname">wrap</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#textwrap.TextWrapper.wrap" title="Permalink to this definition">¶</a></dt>
<dd>Wraps the single paragraph in <em>text</em> (a string) so every line is at most
<a title="textwrap.TextWrapper.width" class="reference internal" href="#textwrap.TextWrapper.width"><tt class="xref docutils literal"><span class="pre">width</span></tt></a> characters long.  All wrapping options are taken from
instance attributes of the <a title="textwrap.TextWrapper" class="reference internal" href="#textwrap.TextWrapper"><tt class="xref docutils literal"><span class="pre">TextWrapper</span></tt></a> instance. Returns a list
of output lines, without final newlines.</dd></dl>

<dl class="method">
<dt id="textwrap.TextWrapper.fill">
<tt class="descname">fill</tt><big>(</big><em>text</em><big>)</big><a class="headerlink" href="#textwrap.TextWrapper.fill" title="Permalink to this definition">¶</a></dt>
<dd>Wraps the single paragraph in <em>text</em>, and returns a single string
containing the wrapped paragraph.</dd></dl>

</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="stringio.html"
                                  title="previous chapter">8.5. <tt class="docutils literal docutils literal docutils literal"><span class="pre">StringIO</span></tt> &#8212; Read and write strings as files</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="codecs.html"
                                  title="next chapter">8.8. <tt class="docutils literal docutils literal"><span class="pre">codecs</span></tt> &#8212; Codec registry and base classes</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/library/textwrap.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="codecs.html" title="8.8. codecs — Codec registry and base classes"
             >next</a> |</li>
        <li class="right" >
          <a href="stringio.html" title="8.5. StringIO — Read and write strings as files"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="../index.html">Python v2.6.2 documentation</a> &raquo;</li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="strings.html" >8. String Services</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 1990-2009, Python Software Foundation.
      Last updated on Apr 15, 2009.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.1.
    </div>
  </body>
</html>